package com.dongdongsongcan.app.activity;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dongdongsongcan.app.DDApp;
import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.adapter.AreaAdapter;
import com.dongdongsongcan.app.common.DDMessageDef;
import com.dongdongsongcan.app.control.AddressControl;
import com.dongdongsongcan.app.control.SearchControl;
import com.dongdongsongcan.app.control.UserControl;
import com.dongdongsongcan.app.control.bean.AddressData;
import com.dongdongsongcan.app.control.bean.AddressData.Address;
import com.dongdongsongcan.app.control.bean.AreaData;
import com.dongdongsongcan.app.control.bean.DDLocation;
import com.dongdongsongcan.app.utils.LocationUtil;
import com.dongdongsongcan.app.view.AddressItem;
import com.su.dolphin.control.task.CtrlTask;
import com.su.dolphin.control.task.CtrlTask.IControlResponse;
import com.su.dolphin.core.notification.DpMessageCenter;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.activity.BaseActivity;

public class LocationChooseActivity extends BaseActivity {
    private LinearLayout mChoosearea_ll_containers;
    private TextView mAddresschoose_current_location;
    private EditText mChoosearea_et;
    private ListView mSearch_list;
    private AreaAdapter mAreaAdapter;
    private View mChoosearea_btn_clear;
    private View mTv_myaddress;

    public static void startFrom(Context context) {
        DpActivityManager.i().startActivity(context,
                LocationChooseActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DpMessageCenter.i().register(this, DDMessageDef.N_LOCATION_CHANGE);
        setContentView(R.layout.activity_address_choose);
        getDpTitleBar().setTitle("选择送货地址");

    }

    @Override
    public void initView() {
        mChoosearea_ll_containers = (LinearLayout) findViewById(R.id.choosearea_ll_containers);
        mAddresschoose_current_location = (TextView) findViewById(R.id.addresschoose_current_location);
        mChoosearea_et = (EditText) findViewById(R.id.choosearea_et);
        mSearch_list = (ListView) findViewById(R.id.search_list);
        mChoosearea_btn_clear = findViewById(R.id.choosearea_btn_clear);
        mTv_myaddress = findViewById(R.id.tv_myaddress);
        mAreaAdapter = new AreaAdapter(this);
        mSearch_list.setAdapter(mAreaAdapter);
    }

    @Override
    public void setListener() {

        findViewById(R.id.addresschoose_location_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LocationUtil.getLocation(DDApp.getContext());

            }
        });
        mSearch_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AreaData.Area area = mAreaAdapter.getData(position);
                DDLocation ddLocaton = new DDLocation();
                ddLocaton.setSite_y(Double.valueOf(area.getSite_y()));
                ddLocaton.setSite_x(Double.valueOf(area.getSite_x()));
                ddLocaton.setArea_name(area.getArea_name());
                DDApp.saveDDLocation(ddLocaton);
                LocationChooseActivity.this.finish();
            }
        });
        mAddresschoose_current_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LocationChooseActivity.this.finish();
            }
        });
        mChoosearea_btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mChoosearea_et.setText("");
            }
        });
        mChoosearea_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    mSearch_list.setVisibility(View.INVISIBLE);
                } else {
                    mSearch_list.setVisibility(View.VISIBLE);
                    String keyWord = s.toString();
                    if (TextUtils.isEmpty(keyWord)) return;
                    SearchControl.search(keyWord, new CtrlTask.IControlResponse<AreaData>() {
                        @Override
                        public void onControlResponse(AreaData data) {
                            if (data.isOk()) {
                                List<AreaData.Area> areas = data.getResult();
                                mAreaAdapter.setData(areas);
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    public void initData() {
        mAddresschoose_current_location.setText(DDApp.getDDLocation().getArea_name());
        AddressControl.i().user_address_list(new IControlResponse<AddressData>() {

            @Override
            public void onControlResponse(AddressData data) {
                TipsUtil.toast(data.getMsg());
                if (data.isOk()) {
                    mChoosearea_ll_containers.removeAllViews();
                    mTv_myaddress.setVisibility(View.VISIBLE);
                    List<Address> addresses = data.getResult();
                    for (int i = 0; i < addresses.size(); i++) {
                        AddressItem addressItemView = new AddressItem(
                                LocationChooseActivity.this);
                        final Address address = addresses.get(i);
                        addressItemView.setInfo(addresses.get(i));
                        mChoosearea_ll_containers.addView(addressItemView.getView());
                        addressItemView.getView().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                DDLocation ddLocation = new DDLocation();
                                if (address.getSite_x() != null) {
                                    ddLocation.setSite_y(Double.valueOf(address.getSite_y()));
                                    ddLocation.setSite_x(Double.valueOf(address.getSite_x()));
                                    ddLocation.setArea_name(address.getInfo_area());
                                    DDApp.saveDDLocation(ddLocation);
                                    LocationChooseActivity.this.finish();
                                    UserControl.i().saveDefaultAddress(address);
                                }

                            }
                        });
                    }
                }

            }
        });
    }

    @Override
    public void onNotify(DpMessageCenter.MCMessage notification) {
        if (notification.notificationID == DDMessageDef.N_LOCATION_CHANGE) {
            mAddresschoose_current_location.setText(DDApp.getDDLocation().getArea_name());
        }
    }
}
